<?php 
class User_IndexController extends Lin_Template
{
	public function userList()
	{
		$grid = new Lin_Grid('userlist');
		$grid->setQuery("SELECT 'op' as op, user_id, user_account, user_email FROM `user`");
		$grid->setGrid(array('caption' => 'User List',
								'url' => '/user/index/_userlist',
								'sortname' => 'user_id'
						));
		$grid->setColModel(0, array('width' => 30, 'align' => 'center'))->setColModel(1,  array('width' => 30));
		return $grid->html();
	}
	
	public function _userListAction()
	{
		$grid = new Lin_Grid('userlist');
		$grid->setQuery("SELECT 'op' as op, user_id, user_account, user_email FROM `user`");
		$grid->setKey('user_id');
		$grid->setKeyLink(0, '<a href="/user/index/edit/{key}">Edit</a> <a href="{key}" class="user-del">Del</a>');
		echo $grid->json();
	}
	
	public function indexAction()
	{
		$msg = '';
		isset($_POST['user_account']) ? $userAccount = trim($_POST['user_account']) : '';
		isset($_POST['user_pwd']) ? $userPwd = $_POST['user_pwd'] : '';
		isset($_POST['user_pwd1']) ? $userPwd1 = $_POST['user_pwd1'] : '';
		isset($_POST['user_email']) ? $userEmail = trim($_POST['user_email']) : '';
				
		if (isset($_POST['action']) && $_POST['action'] == 'add')
		{
			if (!$userAccount)
				$msg .= 'Account is empty.<br/>';
			if (!$userPwd || $userPwd != $userPwd1)
				$msg .= "Password is empty or Confirm password.<br/>";
			if (!$userEmail)
				$msg .= "Email is empty.<br/>";
			if ($msg)
				Lin_Dialog::msg('Add Account', 'Add account unsuccessfully !  <br/>' . $msg, -1);
			
			$user = new Model_User_User();
			$data = array(
						'user_account' => $userAccount,
						'user_pwd' => md5($userPwd),
						'user_email' => $userEmail
					);
			$user->put($data);
			Lin_Dialog::msg('Add Account', 'Add account successfully ! ', 1, '/user/');
		}
		elseif (isset($_POST['action']) && $_POST['action'] == 'edit')
		{
			if (!$userAccount)
				$msg .= 'Account is empty.<br/>';
			if ($userPwd != $userPwd1)
				$msg .= "Confirm password.<br/>";
			if (!$userEmail)
				$msg .= "Email is empty.<br/>";
			if ($msg)
				Lin_Dialog::msg('Edit Account', 'Edit account unsuccessfully !  <br/>' . $msg, -1);
			
			$user = new Model_User_User();
			$data = array(
						'user_account' => $userAccount,
						'user_email' => $userEmail
					);
			if ($userPwd)
			{
				$data['user_pwd'] = md5($userPwd);
			}
			$user->set($_REQUEST['user_id'], $data);
			Lin_Dialog::msg('Edit Account', 'Edit account successfully ! ', 1, '/user/');
	
		}
	}
	
	public function editAction()
	{
		$request = Lin_Request::getInstance();
		$args = $request->getArgs();
		$id = $args['3'];
		$user = new Model_User_User();
		$this->userEditData = $user->get($id);
	}
	
	public function delAction()
	{
		$request = Lin_Request::getInstance();
		$args = $request->getArgs();
		$id = $args['3'];
		$user = new Model_User_User();
		if ($user->remove($id))
		{
			Lin_Dialog::msg('Del Account', 'Del account successfully ! ', 1, '/user/');
		}
	}
	
	public function testAction()
	{
		
	}
	
	public function test()
	{
		return '<div style="width:100%; height:200px; background:#F00;"></div>';
	}	
}